﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Category management"
		PageController.AuthorizedRoles =  Array("admin")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Category management</H2>
	

	
	<%Page.Execute%>	
	<Span Class="Caption">Category Edit<HR></Span>
	<a href="javascript:clasp.form.doPostBack('SetVisibility', 'Panel')">Hide/Show Panel!</a>

	<%panel%>
	
	
<%End Function%>

<%  '
	Dim txtName
	Dim txtDescription
	Dim cboParent
	Dim chkMenu
	
	Dim cboTarget
	Dim chkDisplay
	Dim txtUrl
	Dim Panel

	Dim mValidation

	Dim cmdSave	
	Dim recordID
	Dim parentID
	Dim parentLevel
		
	Public Function Page_Init()
		recordID = 0  'Init
		parentLevel =0
		
		Set  txtName	= New_ServerTextBoxEX("txtName",50,50)
		Set  txtDescription	= New_ServerTextBoxEX("txtDescription",100,100)
	
		Set  cboParent		= New_ServerDropDown("cboParent")
		
		Set  chkMenu	= new_serverCheckBoxList("chkMenu")
		Set  txtUrl	= New_ServerTextBoxEX("txtUrl",50,50)
		
		Set chkDisplay = New_ServerCheckBox("chkDisplay")
		Set cboTarget = New_ServerDropDown("cboTarget")
		Set cmdSave = New_ServerLinkButton("cmdSave")		
		
		Set mValidation = New ServerValidationSummary
		Set panel = New_ServerPanel("Panel",500,400)
		
	End Function

	Public Function Page_Controls_Init()		
		cmdSave.Text = "Save"
		
		Dim rs
		Set rs = Nothing
		If Application("CHKMENUS") = "" Then
			Set rs = GetRecordset("SELECT * FROM menu")					
			Call CacheListItemsCollection(rs,"name","id","CHKMENUS",False,"")
			Set rs = Nothing
		End If
		SetListItemsCollectionFromCache chkMenu,"CHKMENUS"		
		chkMenu.RepeatColumns=4
		
		cboTarget.Items.Append "New Window","_blank",False
		cboTarget.Items.Append "In This Window","",True
		
		
		
		
		'Response.Write("SELECT id, name FROM category where level <= " & parentLevel )
		txtDescription.Mode=3
		txtDescription.Rows = 5
		txtDescription.Cols = 40
		panel.Mode = 2
		'panel.OverFlow = "auto" 'scroll?
		panel.PanelTemplate = "RenderPanel"
	End Function

	Public Function Page_Load()
	
		If Not Page.IsPostBack Then
			If Request.QueryString("parentid")<>"" Then
				parentID = Clng(Request.QueryString("parentid"))
				Dim rsParent
				Dim sSQLparent
				sSQLparent ="Select * from category Where id= " & parentID
				Set rsParent = DBLayer.GetRecordSet(sSQLparent)
				parentLevel = rsParent("level").Value
				
				
			End if
			If Request.QueryString("id")<>"" Then
				recordID = Clng(Request.QueryString("id"))			
				 
			End if
			Call LoadRecord()
						
		End If	
	End Function
	
	Public Function Page_LoadViewState()
		recordID = Page.ViewState.GetValue("ID") 'Get from viewstate	
		parentID = Page.ViewState.GetValue("parentID") 'Get from viewstate	
		parentLevel = Page.ViewState.GetValue("parentLevel") 'Get from viewstate	

	End Function
	Public Function Page_PreRender()
		Page.ViewState.Add "ID",recordID 'Add to viewstate :-)
		Page.ViewState.Add "parentID",parentID 'Add to viewstate :-)
		Page.ViewState.Add "parentLevel",parentLevel 'Add to viewstate :-)
	End Function

	Public Function cmdSave_OnClick()
		Dim sSQL
		Dim rs
		Dim mode 
		
		Call Validate()
		If Not mValidation.IsValid Then
			Exit Function
		End If
		
		sSQL = "SELECT * FROM category Where id=" & recordID
		
		
		Set rs = DBLayer.GetRecordSet(sSQL)
		
		
		If rs.RecordCount = 0 Then
			rs.AddNew
			mode = "new"
		End If
		
		rs("name").Value	= txtName.Text
		rs("description").Value    = txtDescription.Text	
		rs("parentid").Value		= cboParent.Value
		
		rs("status").Value = 1
		rs("level").Value = parentLevel +1
		rs("isdeleted").Value = 0
		
	
		DBLayer.UpdateRecordSet rs
		
		If Mode = "new" Then
			'Get Category ID
			sSQL = "SELECT max(id) as id FROM category"			
			Set rs = DBLayer.GetRecordSet(sSQL)	
			recordID = rs("id").Value	
			
		
		
		End If
		
		'update to menuitem
		Dim x
			
		For x = 0 To chkMenu.Items.Count-1
			
			If chkMenu.Items.IsSelected(x) Then
				sSQL = "SELECT * from menuitem where categoryid= " & recordID & " and menuid = " &  chkMenu.Items.GetValue(x)
				Set rs = DBLayer.GetRecordSet(sSQL)
				If rs.RecordCount = 0 Then
					rs.AddNew					
				End If
				
				rs("menuid").Value = chkMenu.Items.GetValue(x)
				rs("categoryid").Value = recordID
				rs("url").Value = txtUrl.Text
				rs("display").Value = chkDisplay.Checked
				rs("target").Value = cboTarget.Value
				DBLayer.UpdateRecordSet rs
				'Response.Write(chkMenu.Items.GetText(x) & " Is Selected, Value:" &  chkMenu.Items.GetValue(x) & "<BR>")
			Else
				If Mode <> "new" Then
					DBLayer.ExecuteSQL "Delete  from menuitem where categoryid = " & recordID & "and menuid = " &  chkMenu.Items.GetValue(x)
				End If
			End If
		Next
		
		
		
		If mValidation.IsValid Then
			Response.Redirect "Category.asp?parentid=" & cboParent.Value
		End If
		
	End Function
	
	Public Function Validate()
		
		'You could use several approaches to validation. I normaly use this approach and modify the control output as appropriate
	
		'(object,strFriendlyName, intDataType, bolRequired, strDefaultValue)
		mValidation.Validate 	txtName,"Category Name", VALIDATION_DATATYPE_STRING, True , ""
		mValidation.Validate 	cboParent,"Parent", VALIDATION_DATATYPE_STRING, True , ""
		
		
	End Function

	Public Function LoadRecord()
		Dim sSQL
		Dim rs
		'Response.Write("SELECT id, name FROM category where level <= (select level from category where id =" & parentID & ")")
		cboParent.Bind GetRecordSet("SELECT id, name FROM category where level <= " & parentLevel ),"id","name","",True
		
		sSQL = "SELECT * FROM category Where id=" & recordID
		Set rs = GetRecordSet(sSQL)
		
		If rs.RecordCount > 0 Then
			txtName.Text = rs("name").Value
			txtDescription.Text  = rs("description").Value
			cboParent.Items.SetSelectedByValue rs("parentid").Value,True	
		Else
			cboParent.Items.SetSelectedByValue parentID,True	
		End If
		'Get data from menuitem
		sSQL = "SELECT * from menuitem where categoryid= " & recordID
		Set rs = GetRecordSet(sSQL)
		While Not rs.EOF
			txtUrl.Text = rs("url").Value
			chkDisplay.Checked = rs("display").Value
			cboTarget.Items.SetSelectedByValue rs("target").Value, True
			chkMenu.Items.SetSelectedByValue rs("menuid").Value, True
			rs.MoveNext
		Wend
		
			
	End Function

	Public Function Panel_SetVisibility(e) 
		Panel.Control.Visible = Not Panel.Control.Visible
	End Function
%>


<%Function RenderPanel() %>
<%mValidation%>
	<table border=0 ID="Table1">
		<tr valign=top><td>Category Name </td><td><%txtName%></td></tr>
		<tr valign=top><td>Description</td><td><%txtDescription%></td></tr>
		<tr valign=top><td>Parent</td><td><%cboParent%></td></tr>
		<tr><td colspan="2"><HR><b>Setting Menu</b></td></tr>
		<tr valign=top><td>Possition</td><td><%chkMenu%></td></tr>
		<tr valign=top><td>Display</td><td><%chkDisplay%></td></tr>
		<tr valign=top><td>Url</td><td><%txtUrl%></td></tr>
		<tr valign=top><td>Target</td><td><%cboTarget%></td></tr>
	
		<tr><td colspan=2 align=left><a href="Category.asp?parentid=<%Response.Write(parentID)%>"><HR>Back</a> | <%cmdSave%></td></tr>
	</table>
	
<%End Function%>
